//
//  Util.m
//  InterventixApp
//
//  Created by Diego Falcinelli on 08/05/13.
//
//

#import "Util.h"
#import "Define.h"

@implementation Util

+(NSData*) hexToBytes: (NSString *) s {
    NSMutableData* data = [NSMutableData data];
    int idx;
    for (idx = 0; idx+2 <= s.length; idx+=2) {
        NSRange range = NSMakeRange(idx, 2);
        NSString* hexStr = [s substringWithRange:range];
        NSScanner* scanner = [NSScanner scannerWithString:hexStr];
        unsigned int intValue;
        [scanner scanHexInt:&intValue];
        [data appendBytes:&intValue length:1];
    }
    return data;
}
+(NSString *) TipologiaInterventoToName:(NSString *) code{
    if([code isEqualToString:@"request"]){
        return @"Su richiesta";
    }
    if([code isEqualToString:@"programmed"]){
        return @"Programmata";
    }
    return @"Error";
}
+(NSString *) TipologiaInterventoToCode:(NSString *) name{
    if([name isEqualToString:@"Su richiesta"]){
        return @"request";
    }
    if([name isEqualToString:@"Programmata"]){
        return @"programmed";
    }
    return @"Error";
}

+(NSString *) ModalitaInterventoToName:(NSString *) code{
    if([code isEqualToString:@"contract"]){
        return @"Contrattualizzato";
    }
    if([code isEqualToString:@"payment"]){
        return @"Pagamento Diretto";
    }
    if([code isEqualToString:@"postsale"]){
        return @"Post-Vendita (Garanzia)";
    }
    if([code isEqualToString:@"rentuse"]){
        return @"Comodato d'uso";
    }
    return @"Error";
}
+(NSString *) ModalitaInterventoToCode:(NSString *) name{
    if([name isEqualToString:@"Contrattualizzato"]){
        return @"contract";
    }
    if([name isEqualToString:@"Pagamento Diretto"]){
        return @"payment";
    }
    if([name isEqualToString:@"Post-Vendita (Garanzia)"]){
        return @"postsale";
    }
    if([name isEqualToString:@"Comodato d'uso"]){
        return @"rentuse";
    }
    return @"Error";
}

+(NSDate *)roundDateToMinutes:(NSDate *)date andMinutes:(int) min{
    // Set up flags.
    unsigned unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit;
    // Extract components.
    NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:date];
    // Set the minute to the nearest 15 minutes.
    [comps setMinute:((([comps minute] - (min/2) ) / min ) * min ) + min];
    // Zero out the seconds.
    [comps setSecond:0];
    // Construct a new date.
    return [[NSCalendar currentCalendar] dateFromComponents:comps];
}




+(NSNumber *) getNextClienteId{
    if([[NSUserDefaults standardUserDefaults] objectForKey:CONT_CLIENT_ID] == nil){
        [[NSUserDefaults standardUserDefaults] setObject:@-1 forKey: CONT_CLIENT_ID];
    }
    NSNumber *nextid = [[NSUserDefaults standardUserDefaults] objectForKey:CONT_CLIENT_ID];
    NSNumber *pp = @([nextid intValue]-1);
    [[NSUserDefaults standardUserDefaults] setObject:pp forKey: CONT_CLIENT_ID];
    return nextid;
}

+(NSNumber *) getNextInterventoId{
    if([[NSUserDefaults standardUserDefaults] objectForKey:CONT_INTERVENT_ID] == nil){
        [[NSUserDefaults standardUserDefaults] setObject:@-1 forKey: CONT_INTERVENT_ID];
    }
    NSNumber *nextid = [[NSUserDefaults standardUserDefaults] objectForKey:CONT_INTERVENT_ID];
    NSNumber *pp = @([nextid intValue]-1);
    [[NSUserDefaults standardUserDefaults] setObject:pp forKey: CONT_INTERVENT_ID];
    return nextid;
}
+(NSNumber *) getNextDettaglioId{
    if([[NSUserDefaults standardUserDefaults] objectForKey:CONT_DETT_ID] == nil){
        [[NSUserDefaults standardUserDefaults] setObject:@-1 forKey: CONT_DETT_ID];
    }
    NSNumber *nextid = [[NSUserDefaults standardUserDefaults] objectForKey:CONT_DETT_ID];
    NSNumber *pp = @([nextid intValue]-1);
    [[NSUserDefaults standardUserDefaults] setObject:pp forKey: CONT_DETT_ID];
    return nextid;
}


+(void) setCellDefault:(UITableViewCell *)cell{
    [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
}


+(void) setCellModified:(UITableViewCell *)cell{
    [cell setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.3 alpha:1]];
}
+(void) setCellNew:(UITableViewCell *)cell{
    [cell setBackgroundColor:[UIColor colorWithRed:0.3 green:0.6 blue:0.3 alpha:1]];
    cell.textLabel.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    cell.detailTextLabel.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
}
+(void) setCellDisable:(UITableViewCell *)cell{
    [cell setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1]];
}

+(void) setCellAlert:(UITableViewCell *)cell{
    [cell setBackgroundColor:[UIColor colorWithRed:0.8 green:0.3 blue:0.3 alpha:1]];
    cell.textLabel.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    //cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
    cell.detailTextLabel.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    //cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:16];
}





+(BOOL) isLockBuffer{
    if([[NSUserDefaults standardUserDefaults] objectForKey:@"isLockBuffer"] == nil){
        return NO;
    }else{
        
        if([[[NSUserDefaults standardUserDefaults] objectForKey:@"isLockBuffer"] isEqualToString:@"lock"]){
            return YES;
        }else{
            return NO;
        }
    }
}

+(void) lockBuffer{
    [[NSUserDefaults standardUserDefaults] setObject:@"lock" forKey:@"isLockBuffer"];
}

+(void) unlockBuffer{
    [[NSUserDefaults standardUserDefaults] setObject:@"unlock" forKey:@"isLockBuffer"];
}




+(void) navigatorTo{
    CLLocationCoordinate2D endingCoord = CLLocationCoordinate2DMake(40.446947, -102.047607);
    MKPlacemark *endLocation = [[MKPlacemark alloc] initWithCoordinate:endingCoord addressDictionary:nil];
    
    MKMapItem *endingItem = [[MKMapItem alloc] initWithPlacemark:endLocation];
    [endingItem setName:@"My Place"];
    
    
    NSMutableDictionary *launchOptions = [[NSMutableDictionary alloc] init];
    [launchOptions setObject:MKLaunchOptionsDirectionsModeDriving forKey:MKLaunchOptionsDirectionsModeKey];
    
    
    [endingItem openInMapsWithLaunchOptions:launchOptions];
}

@end
